package com.psdi.window;

import javax.swing.*;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
 * 游戏窗口类
 * 用于创建游戏的主窗口
 */
public class GameJFrame extends JFrame {
    public static int GAME_WIDTH = 800; // 游戏窗口宽度
    public static int GAME_HEIGHT = 620; // 游戏窗口高度
    private JPanel panel;
    private KeyListener keyListener = null;
    private MouseListener mouseListener = null;
    private MouseMotionListener mouseMotionListener = null;
    private Thread thread = null;

    public GameJFrame() {
        initComponents();
    }

    public static void setGameWidth(int gameWidth) {
        GAME_WIDTH = gameWidth;
    }

    public static void setGameHeight(int gameHeight) {
        GAME_HEIGHT = gameHeight;
    }

    private void initComponents() {
        setSize(GAME_WIDTH, GAME_HEIGHT);
        setLocationRelativeTo(null);
        setTitle("PSDi Game");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void start() {
        if (panel != null) {
            add(panel);
            if (mouseListener != null) panel.addMouseListener(mouseListener);
        }
        if (keyListener != null) addKeyListener(keyListener);
        if (mouseMotionListener != null) addMouseMotionListener(mouseMotionListener);
        if (thread != null) thread.start();
        setVisible(true);
        if (panel instanceof Runnable) {
            new Thread((Runnable) panel).start();
        }
    }

    public void setPanel(JPanel panel) {
        this.panel = panel;
    }

    public void setKeyListener(KeyListener keyListener) {
        this.keyListener = keyListener;
    }

    public void setMouseListener(MouseListener mouseListener) {
        this.mouseListener = mouseListener;
    }

    public void setMouseMotionListener(MouseMotionListener mouseMotionListener) {
        this.mouseMotionListener = mouseMotionListener;
    }

    public void setThread(Thread thread) {
        this.thread = thread;
    }
}
